package com.ruoyi.basebill.util;

/**
 * @projectName: wfz
 * @package: com.ruoyi.basebill.util
 * @className: OmsUtil
 * @author: Eric
 * @description: TODO
 * @date: 2023/2/20 17:28
 * @version: 1.0
 */

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.*;
import com.ruoyi.basebill.service.exception.DataNullException;
import com.ruoyi.basebill.service.impl.OmsOtherissuebillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsPurinwarehsbillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsSaleissuebillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsStocktransferbillServiceImpl;
import com.ruoyi.basebill.task.OmsDeliverbillTask;
import com.ruoyi.basebill.task.OmsReceiptBillTask;
import com.ruoyi.basebill.task.OmsReceiptBillTaskNew;
import com.ruoyi.basebill.task.OmsTransferbillTask;
import com.ruoyi.basedata.domain.*;
import com.ruoyi.basedata.service.*;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.*;

@Component
public class OmsUtil {

    @Autowired
    private IBdCustomerService bdCustomerService;

    @Autowired
    private IOmsTransferbillService omsTransferbillService;

    @Autowired
    private IOmsSalereturnbillService omsSalereturnbillService;

    @Autowired
    private IOmsSaleissuebillService omsSaleissuebillService;

    @Autowired
    private IOmsReceiptbillService omsReceiptbillService;

    @Autowired
    private IOmsDeliverbillService omsDeliverbillService;

    @Autowired
    private IOmsOrgunitService omsOrgunitService;

    @Autowired
    private IBdWarehouseService bdWarehouseService;

    @Autowired
    private IBdWdtwarehouseconfigService bdWdtwarehouseconfigService;

    @Autowired
    private IBdSupplierService bdSupplierService;

    @Autowired
    private OmsUtil omsUtil;

    @Autowired
    private IBdMaterialService bdMaterialService;

    @Autowired
    private OmsStocktransferbillServiceImpl omsStocktransferbillService;

    @Autowired
    private OmsOtherissuebillServiceImpl omsOtherissuebillService;

    @Autowired
    private OmsPurinwarehsbillServiceImpl omsPurinwarehsbillService;

    @Autowired
    private OmsTransferbillTask omsTransferbillTask;

    @Autowired
    private OmsReceiptBillTask omsReceiptBillTask;

    @Autowired
    private OmsReceiptBillTaskNew omsReceiptBillTaskNew;

    @Autowired
    private OmsDeliverbillTask omsDeliverbillTask;

    public BdCustomer getCustomer(String customerCode){

        BdCustomer bdCustomer = new BdCustomer();
        BdCustomer customer = null;
        bdCustomer.setCustomernumber(customerCode);
        List<BdCustomer> bdCustomers = bdCustomerService.selectBdCustomerList(bdCustomer);
        if(bdCustomers.size()>0){
            customer = bdCustomers.get(0);
        }
        return customer;
    }

    public OmsOrgunit getOmsOrgunit(String omsOrgunitCode){
        OmsOrgunit omsOrgunit = new OmsOrgunit();
        omsOrgunit.setOrgnumber(omsOrgunitCode);
        OmsOrgunit orgunit = null;

        List<OmsOrgunit> omsOrgunits = omsOrgunitService.selectOmsOrgunitList(omsOrgunit);
        if(omsOrgunits.size()>0){
            orgunit = omsOrgunits.get(0);
        }

        return orgunit;

    }

    public BdWarehouse getBdWarehouse(String warehouseCode){
        BdWarehouse bdWarehouse = new BdWarehouse();
        BdWarehouse warehouse = null;
        bdWarehouse.setWarehousenumber(warehouseCode);
        List<BdWarehouse> bdWarehouses = bdWarehouseService.selectBdWarehouseList(bdWarehouse);
        if(bdWarehouses.size()>0){
            warehouse = bdWarehouses.get(0);
        }
        return warehouse;
    }

    public BdWdtwarehouseconfig getBdWdtWarehouse(String warehouseCode){
        BdWdtwarehouseconfig bdWarehouse = new BdWdtwarehouseconfig();
        BdWdtwarehouseconfig warehouse = null;
        bdWarehouse.setWdtwarenumber(warehouseCode);
        List<BdWdtwarehouseconfig> bdWdtwarehouseconfigs = bdWdtwarehouseconfigService.selectBdWdtwarehouseconfigList(bdWarehouse);
        if(bdWdtwarehouseconfigs.size()>0){
            warehouse = bdWdtwarehouseconfigs.get(0);
        }
        return warehouse;
    }

    public OmsSalereturnbill rebuildSalereturn(OmsSalereturnbill omsSalereturnbill,String type){

        OmsSalereturnbill rebuildSalereturn = new OmsSalereturnbill();
        BeanUtils.copyProperties(omsSalereturnbill,rebuildSalereturn);
        List<OmsSalereturnbillentry> omsSalereturnbillentryList = rebuildSalereturn.getOmsSalereturnbillentryList();

        ArrayList<OmsSalereturnbillentry> omsSalereturnbillentries = new ArrayList<>();


        for (OmsSalereturnbillentry omsSalereturnbillentry : omsSalereturnbillentryList) {
            BigDecimal qualityundertakeqty = omsSalereturnbillentry.getQualityundertakeqty();//品控承担数量
            BigDecimal warehousenoneqty = omsSalereturnbillentry.getWarehousenoneqty();//仓库承担无实物数量
            String returnwarehousecode = omsSalereturnbillentry.getReturnwarehousecode(); //退入仓库
            String virtualreturnwarehousecode = omsSalereturnbillentry.getVirtualreturnwarehousecode();



            if("1".equals(type)){ //品控承担数量
                if(qualityundertakeqty.compareTo(BigDecimal.ZERO)>0){
                    omsSalereturnbillentries.add(omsSalereturnbillentry);
                }
            }else{ //仓库承担无实物数量
                //update by wyx :7、如果仓库无实物退货数量大于0且退入仓库编码不等于虚拟退货仓库编码，则生成一张库存调拨单。否则跳过此步骤。
                if(warehousenoneqty.compareTo(BigDecimal.ZERO)>0&&!returnwarehousecode.equals(virtualreturnwarehousecode)){
                    omsSalereturnbillentries.add(omsSalereturnbillentry);
                }
            }


        }
        rebuildSalereturn.setOmsSalereturnbillentryList(omsSalereturnbillentries);
        return rebuildSalereturn;
    }


    public Map<String,String> judOutWareHouse(OmsSalereturnbill omsSalereturnbill, OmsSalereturnbillentry omsSalereturnbillentry) throws DataNullException {
        String returnwarehousecode = omsSalereturnbillentry.getReturnwarehousecode();
        String invoicecode = omsSalereturnbill.getInvoicecode();


        BdWarehouse bdWarehouse = getBdWarehouse(returnwarehousecode);
        if(bdWarehouse==null){
            throw new DataNullException("明细退入仓库编码有误"+returnwarehousecode);
        }
        String storageorgnumber = bdWarehouse.getStorageorgnumber(); //明细仓库的库存组织
        if(StringUtils.isEmpty(storageorgnumber)){
            throw new DataNullException("明细退入仓库"+returnwarehousecode+"没有库存组织");

        }
        HashMap<String, String> map = new HashMap<>();
        if(storageorgnumber.equals(invoicecode)){
            String warehousecode = omsSalereturnbillentry.getReturnwarehousecode();
            String returnwarehousename = omsSalereturnbillentry.getReturnwarehousename();

            map.put("code",warehousecode);
            map.put("name",returnwarehousename);
            return map;
        }else{
            OmsOrgunit omsOrgunit = getOmsOrgunit(invoicecode);
            String transferwhnumber = omsOrgunit.getTransferwhnumber();
            String transferwhname = omsOrgunit.getTransferwhname();
            if (transferwhnumber==null){
                String customercode = omsSalereturnbill.getCustomercode();
                BdCustomer customer = getCustomer(customercode);
                String storewhnumber = customer.getStorewhnumber();
                String storewhname = customer.getStorewhname();
                if(StringUtils.isEmpty(storewhnumber)){
                   return null;
                }
                map.put("code",storewhnumber);
                map.put("name",storewhname);
            }else{
                map.put("code",transferwhnumber);
                map.put("name",transferwhname);
            }
            return map;
        }
    }

    /**
     * 获取一级调拨单仓库
     * invoiceCode和第一行退入仓库returnWarehouseCode的库存组织编码相同，则取传过来的returnWarehouseCode；
     * 2、否则取invoiceCode的中转仓。
     * @param omsSalereturnbill
     * @param omsSalereturnbillentry
     * @return
     */
    public Map<String,String> judReWareHouse(OmsSalereturnbill omsSalereturnbill, OmsSalereturnbillentry omsSalereturnbillentry) {

        HashMap<String, String> map = new HashMap<>();
        String invoicecode = omsSalereturnbill.getInvoicecode();
        String returnwarehousecode = omsSalereturnbillentry.getReturnwarehousecode();
        BdWarehouse bdWarehouse = getBdWarehouse(returnwarehousecode);
        String storageorgnumber = bdWarehouse.getStorageorgnumber();


        //invoiceCode和第一行退入仓库returnWarehouseCode的库存组织编码相同，则取传过来的returnWarehouseCode；
        if(invoicecode.equals(storageorgnumber)){
            map.put("code",bdWarehouse.getWarehousenumber());
            map.put("name",bdWarehouse.getWarehousename());
            return map;
        }else{
            OmsOrgunit omsOrgunit = getOmsOrgunit(invoicecode);
            String transferwhnumber = omsOrgunit.getTransferwhnumber();
            String transferwhname = omsOrgunit.getTransferwhname();
            map.put("code",transferwhnumber);
            map.put("name",transferwhname);
            return map;
        }

    }

    /**
     * 根据组织获取供应商
     * @param orgCode
     * @return
     */
    public BdSupplier getSupplier(String orgCode){
        BdSupplier bdSupplier = new BdSupplier();
        BdSupplier supplier = null;
        bdSupplier.setInternalcompanynumber(orgCode);

        List<BdSupplier> bdSuppliers = bdSupplierService.selectBdSupplierList(bdSupplier);
        if(bdSuppliers.size()>0){
            supplier = bdSuppliers.get(0);
        }
        return supplier;
    }

    /**
     * 第一张：如果是内部客户，看退出仓库不为空则取退出仓库，否则取客户的分公司门店仓。外部客户：取第2步生成的那个销售出库单的仓库。
     * 其他的：storageOrgNumber的中转仓
     * @param omsSalereturnbill
     * @param isFirst
     */
    public Map<String,String> getWareHouseForPur(OmsSalereturnbill omsSalereturnbill,OmsSalereturnbillentry omsSalereturnbillentry,Boolean isFirst,String UnitCode) throws DataNullException {
        HashMap<String, String> map = new HashMap<>();
        String customercode = omsSalereturnbill.getCustomercode();
        if (isFirst){
            BdCustomer customer = omsUtil.getCustomer(customercode);
            Long isinternal = customer.getIsinternal();
            Long iscostcenter = customer.getIscostcenter();
            //是否内部客户
            if(isinternal == 1l||iscostcenter==1l){
                String exitwarehousecode = omsSalereturnbill.getExitwarehousecode();
                //看退出仓库不为空则取退出仓库
                if(!StringUtils.isEmpty(exitwarehousecode)){
                    String exitwarehousename = omsSalereturnbill.getExitwarehousename();
                    map.put("code",exitwarehousecode);
                    map.put("name",exitwarehousename);
                    return map;
                }else{
                    String storewhnumber = customer.getStorewhnumber();
                    String storewhname = customer.getStorewhname();
                    map.put("code",storewhnumber);
                    map.put("name",storewhname);
                    return map;
                }
            }else{
                return judOutWareHouse(omsSalereturnbill,omsSalereturnbillentry);
            }

        }else{
            OmsOrgunit omsOrgunit = getOmsOrgunit(UnitCode);
            String transferwhnumber = omsOrgunit.getTransferwhnumber();
            String transferwhname = omsOrgunit.getTransferwhname();

            map.put("code",transferwhnumber);
            map.put("name",transferwhname);

            return map;
        }
    }

    public BdCustomer getCustomerByOrg(String orgCode){
        BdCustomer bdCustomer = new BdCustomer();
        BdCustomer customer = null;
        bdCustomer.setInternalcompanynumber(orgCode);
        List<BdCustomer> bdCustomers = bdCustomerService.selectBdCustomerList(bdCustomer);
        if(bdCustomers.size()>0){
            customer = bdCustomers.get(0);
        }
        return customer;
    }

    public BdMaterial getMaterial(String materialCode){
        BdMaterial bdMaterial = new BdMaterial();
        BdMaterial material =null;
        bdMaterial.setMaterialnumber(materialCode);
        List<BdMaterial> bdMaterials = bdMaterialService.selectBdMaterialList(bdMaterial);
        if(bdMaterials.size()>0){
            material =  bdMaterials.get(0);
        }
        return material;
    }

    /**
     * 1、单头storageOrgNumber和分录第一行仓库的库存组织编码相同，则取传过来的returnWarehouseCode；
     * 2、否则取单头storageOrgNumber的中转仓。
     * @param
     * @param omsSalereturnbillentry
     * @param UnitCode
     * @return
     */
    public Map<String,String> getWareHouseForSal(OmsSalereturnbillentry omsSalereturnbillentry,String UnitCode){
        String returnwarehousecode = omsSalereturnbillentry.getReturnwarehousecode();
        BdWarehouse bdWarehouse = getBdWarehouse(returnwarehousecode);
        String storageorgnumber = bdWarehouse.getStorageorgnumber();
        HashMap<String, String> map = new HashMap<>();
        if(UnitCode.equals(storageorgnumber)){
            String returnwarehousename = omsSalereturnbillentry.getReturnwarehousename();

            map.put("code",returnwarehousecode);
            map.put("name",returnwarehousename);
            return map;
        }else{
            OmsOrgunit omsOrgunit = getOmsOrgunit(UnitCode);
            String transferwhname = omsOrgunit.getTransferwhname();
            String transferwhnumber = omsOrgunit.getTransferwhnumber();
            if(StringUtils.isEmpty(transferwhnumber)){
                return null;
            }
            map.put("code",transferwhnumber);
            map.put("name",transferwhname);
            return map;
        }

    }

    public StringBuffer checkIsNull(OmsSalereturnbill omsSalereturnbill){
        StringBuffer buffer = new StringBuffer();
        buffer.append("Over");
        List<OmsSalereturnbillentry> omsSalereturnbillentryList = omsSalereturnbill.getOmsSalereturnbillentryList();
        String customercode = omsSalereturnbill.getCustomercode();
        String exitwarehousecode = omsSalereturnbill.getExitwarehousecode();
        String invoicecode = omsSalereturnbill.getInvoicecode();
        String salescostcentercode = omsSalereturnbill.getSalescostcentercode();
        if(StringUtils.isEmpty(customercode)){
            buffer.append("客户为空;");
        }
        if(StringUtils.isEmpty(invoicecode)){
            buffer.append("结算组织为空;");
        }
        if(!buffer.toString().equals("Over")){
            return buffer;
        }

        BdCustomer customer = omsUtil.getCustomer(customercode);
        if(customer==null){
            buffer.append("客户:"+customercode+"不存在;");
        }
        BdWarehouse bdWarehouse = getBdWarehouse(exitwarehousecode);
        if (bdWarehouse==null){
            buffer.append("仓库:"+exitwarehousecode+"不存在;");
        }
        OmsOrgunit omsOrgunit = getOmsOrgunit(invoicecode);
        if(omsOrgunit==null){
            buffer.append("结算组织:"+invoicecode+"不存在;");
        }
        if(omsSalereturnbillentryList.size()<0){
            buffer.append("明细行为空;");
            return buffer;
        }
        for (OmsSalereturnbillentry omsSalereturnbillentry : omsSalereturnbillentryList) {
            String skucode = omsSalereturnbillentry.getSkucode();
            String returnwarehousecode = omsSalereturnbillentry.getReturnwarehousecode();
            BigDecimal qty = omsSalereturnbillentry.getQty();
            BigDecimal baseqty = omsSalereturnbillentry.getBaseqty();
            BigDecimal qualityundertakeqty = omsSalereturnbillentry.getQualityundertakeqty();
            BigDecimal qualityrefuseqty = omsSalereturnbillentry.getQualityrefuseqty();
            BigDecimal warehouserefuseqty = omsSalereturnbillentry.getWarehouserefuseqty();
            BigDecimal warehousenoneqty = omsSalereturnbillentry.getWarehousenoneqty();
            BigDecimal notaxprice = omsSalereturnbillentry.getNotaxprice();
            BigDecimal notaxamount = omsSalereturnbillentry.getNotaxamount();
            BigDecimal taxprice = omsSalereturnbillentry.getTaxprice();
            BigDecimal taxrate = omsSalereturnbillentry.getTaxrate();

            if(qty==null){
                buffer.append("数量为空");
            }

            if(baseqty==null){
                buffer.append("基本数量为空");
            }

            if(qualityundertakeqty==null){
                buffer.append("品控承担数量数量为空");
            }

            if(qualityrefuseqty==null){
                buffer.append("品控拒绝处理数量为空");
            }

            if(warehouserefuseqty==null){
                buffer.append("仓库拒绝处理数量");
            }
            if(warehousenoneqty==null){
                buffer.append("仓库承担无实物数量");
            }
            if(notaxprice==null){
                buffer.append("不含税价格为空");
            }

            if(notaxamount==null){
                buffer.append("不含税金额为空");
            }
            if(taxprice==null){
                buffer.append("含税单价为空");
            }
            if(taxrate==null){
                buffer.append("税额为空");
            }
            BdMaterial material = getMaterial(skucode);
            if(material==null){
                buffer.append("物料:"+skucode+"不存在;");
            }
            BdWarehouse warehouse = getBdWarehouse(returnwarehousecode);
            if (warehouse==null){
                buffer.append("明细退出仓:"+warehouse+"不存在;");
            }
        }
        return buffer;
    }

    /**
     * 查询下游单据
     * @param srcbillnumber 调拨单单号
     * @return  下游单据
     */
    public Map getDownBill(String srcbillnumber,String type)
    {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sumBillNumber",false);
        OmsPurinwarehsbill omsPurinwarehsbill = new OmsPurinwarehsbill();
        omsPurinwarehsbill.setSrcbillnumber(srcbillnumber);
        omsPurinwarehsbill.setSrcbilltype(type);
        List<OmsPurinwarehsbill> omsPurinwarehsbills = omsPurinwarehsbillService.selectOmsPurinwarehsbillList(omsPurinwarehsbill);
        if(StringUtils.isNotEmpty(omsPurinwarehsbills)){
            hashMap.put("purBill",omsPurinwarehsbills);
            for(OmsPurinwarehsbill omsBill:omsPurinwarehsbills){
                String sumbillnumber = omsBill.getSumbillnumber();
                if(StringUtils.isNotBlank(sumbillnumber)){
                    hashMap.put("sumBillNumber",true);
                }
            }
        }
        OmsSaleissuebill omsSaleissuebill = new OmsSaleissuebill();
        omsSaleissuebill.setSrcbillnumber(srcbillnumber);
        omsSaleissuebill.setSrcbilltype(type);
        List<OmsSaleissuebill> omsSaleissuebills = omsSaleissuebillService.selectOmsSaleissuebillList(omsSaleissuebill);
        if(StringUtils.isNotEmpty(omsSaleissuebills)){
            hashMap.put("salBill",omsSaleissuebills);
            for(OmsSaleissuebill omsBill:omsSaleissuebills){
                String sumbillnumber = omsBill.getSumbillnumber();
                if(StringUtils.isNotBlank(sumbillnumber)){
                    hashMap.put("sumBillNumber",true);
                }
            }
        }
        OmsStocktransferbill omsStocktransferbill = new OmsStocktransferbill();
        omsStocktransferbill.setSrcbillnumber(srcbillnumber);
        omsStocktransferbill.setSrcbilltype(type);
        List<OmsStocktransferbill> omsStocktransferbills = omsStocktransferbillService.selectOmsStocktransferbillList(omsStocktransferbill);
        if(StringUtils.isNotEmpty(omsStocktransferbills)){
            hashMap.put("stockBill",omsStocktransferbills);
            for(OmsStocktransferbill omsBill:omsStocktransferbills){
                String sumbillnumber = omsBill.getSumbillnumber();
                if(StringUtils.isNotBlank(sumbillnumber)){
                    hashMap.put("sumBillNumber",true);
                }
            }
        }
        OmsOtherissuebill omsOtherissuebill = new OmsOtherissuebill();
        omsOtherissuebill.setSrcbillnumber(srcbillnumber);
        omsOtherissuebill.setSrcbilltype(type);
        List<OmsOtherissuebill> omsOtherissuebills = omsOtherissuebillService.selectOmsOtherissuebillList(omsOtherissuebill);
        if(StringUtils.isNotEmpty(omsOtherissuebills)){
            hashMap.put("otherOutBill",omsOtherissuebills);
        }
        return hashMap;
    }

    /**
     * 查询下游单据详情
     * @param id 调拨单id
     * @param type 调拨单类型
     * @return  下游单据
     */
    public Map getDownBillInfo(Long id,String type)
    {
        HashMap<String, Object> hashMap = new HashMap<>();
        switch (type){
            case "销售出库":hashMap.put("销售出库",omsSaleissuebillService.selectOmsSaleissuebillById(id));break;
            case "采购入库":hashMap.put("采购入库",omsPurinwarehsbillService.selectOmsPurinwarehsbillById(id));break;
            case "库存调拨":hashMap.put("库存调拨",omsStocktransferbillService.selectOmsStocktransferbillById(id));break;
            case "其他出库":hashMap.put("其他出库",omsOtherissuebillService.selectOmsOtherissuebillById(id));break;
        }
        return hashMap;
    }

    /**
     * 删除下游单据
     * @param id 调拨单id
     * @param type 调拨单类型
     * @return  下游单据
     */
    public int delDownBill(Long[] id,String type)
    {
        try{
            if(id==null||id.length==0){
                return 0;
            }
            switch (type){
                case "销售出库":
                    omsSaleissuebillService.deleteOmsSaleissuebillByIds(id);
                    break;
                case "采购入库":
                    omsPurinwarehsbillService.deleteOmsPurinwarehsbillByIds(id);
                    break;
                case "库存调拨":
                    omsStocktransferbillService.deleteOmsStocktransferbillByIds(id);
                    break;
                case "其他出库":
                    omsOtherissuebillService.deleteOmsOtherissuebillByIds(id);
                    break;
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    /**
     * 回写上游单据下发状态
     * @param id 调拨单单号
     * @param type 调拨单类型
     * @return  下游单据
     */
    public int updataUpBill(Long id,String type) {
        try{
            switch (type) {
                case "调拨单":
                    OmsTransferbill omsTransferbill = new OmsTransferbill();
                    omsTransferbill.setId(id);
                    omsTransferbill = omsTransferbillService.selectOmsTransferbillById(id);
                    omsTransferbill.setIscreatebill(0L);
                    omsTransferbillService.updateOmsTransferbill(omsTransferbill);
                    break;
                case "收货确认":
                    OmsReceiptbill omsReceiptbill = new OmsReceiptbill();
                    omsReceiptbill.setId(id);
                    omsReceiptbill = omsReceiptbillService.selectOmsReceiptbillById(id);
                    omsReceiptbill.setIscreatebill(0L);
                    omsReceiptbill.setIscreateoutbill(0L);
                    omsReceiptbillService.updateOmsReceiptbill(omsReceiptbill);
                    break;
                case "发货确认":
                    OmsDeliverbill omsDeliverbill = new OmsDeliverbill();
                    omsDeliverbill.setId(id);
                    omsDeliverbill = omsDeliverbillService.selectOmsDeliverbillById(id);
                    omsDeliverbill.setIscreatebill(0L);
                    omsDeliverbillService.updateOmsDeliverbill(omsDeliverbill);
                    break;
                case "销售退货":
                    OmsSalereturnbill omsSalereturnbill = new OmsSalereturnbill();
                    omsSalereturnbill.setId(id);
                    omsSalereturnbill = omsSalereturnbillService.selectOmsSalereturnbillById(id);
                    omsSalereturnbill.setIscreatebill(0L);
                    omsSalereturnbill.setIscreateoutbill(0L);
                    omsSalereturnbillService.updateOmsSalereturnbill(omsSalereturnbill);
                    break;
            }
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    /**
     * 单据下发
     * @param ids 调拨单id集合
     * @param type 调拨单类型
     * @return  下游单据
     */
//    @Transactional
    public int handlePush(Long[] ids,String type) {
        try{
            if("调拨单".equals(type)){
                OmsTransferbill omsTransferbill;
                for(Long id:ids){
                    omsTransferbill = omsTransferbillService.selectOmsTransferbillById(id);
                    if(StringUtils.isNotNull(omsTransferbill)&&omsTransferbill.getIscreatebill()!=1L&&omsTransferbill.getIscreatebill()!=-2L){
                        OmsTransferbill omsTransferbill1 = omsTransferbillTask.pushDownMethod(omsTransferbill);
                        omsTransferbillService.updateOmsTransferbill(omsTransferbill1);
                    }
                }
            }else if("收货确认".equals(type)){
                for(Long id:ids){
                    OmsReceiptbill omsReceiptbill = omsReceiptbillService.selectOmsReceiptbillById(id);
                    if(StringUtils.isNotNull(omsReceiptbill)
                            &&omsReceiptbill.getIscreatebill()!=1L&&omsReceiptbill.getIscreatebill()!=-2L
                    ){
                        OmsReceiptbill omsReceiptbill1 = omsReceiptBillTaskNew.pushDownMethod(omsReceiptbill);//下推非外部销出
                        omsReceiptbillService.updateOmsReceiptbill(omsReceiptbill1);
                    }
                    if(StringUtils.isNotNull(omsReceiptbill)
                            &&omsReceiptbill.getIscreateoutbill()!=1L&&omsReceiptbill.getIscreateoutbill()!=-2L
                    ){
                        OmsReceiptbill omsReceiptbill1 = omsReceiptBillTask.pushDownOutSaleBill(omsReceiptbill);//下推外部销出
                        //此处同理，需要修改原本的修改逻辑，不能用汇总后的分录覆盖原分录
                        omsReceiptbillService.updateOmsReceiptbillHead(omsReceiptbill1);
                    }
                }
            }else if("发货确认".equals(type)){
                OmsDeliverbill omsDeliverbill;
                for(Long id:ids){
                    omsDeliverbill = omsDeliverbillService.selectOmsDeliverbillById(id);
                    if(StringUtils.isNotNull(omsDeliverbill)
                            &&omsDeliverbill.getIscreatebill()!=1L&&omsDeliverbill.getIscreatebill()!=-2L
                    ){
                        OmsDeliverbill omsDeliverbill1 = omsDeliverbillTask.pushDownMethod(omsDeliverbill);
                        omsDeliverbillService.updateOmsDeliverbill(omsDeliverbill1);
                    }
                }
            }else if("销售退货".equals(type)){
                OmsSalereturnbill omsSalereturnbill;
            }
            return 1;
        }catch (Exception e){
            return 0;
        }
    }
}
